<template>
	<el-container>
		<el-aside width="250px" v-if="false">
			<el-container>
				<el-main class="nopadding">
					<blCategoryTree title="活动分类" ref="group" model="ActivityV2" @node-click="groupClick" />
				</el-main>
			</el-container>
		</el-aside>
		<el-container>
			<el-header>
				<div class="left-panel" style="gap:15px;" v-if="type && type!=='待生成'">
					<el-button type="primary" :disabled="selectionBill.length==0" @click="handleJieSuan">确认结算
						{{selectionBill.length>0 ?`(已选中${selectionBill.length})`: '' }}</el-button>
				</div>
				<div v-else class="left-panel" style="gap:15px;">
					<el-button type="primary" plain :disabled="selection.length==0" @click="handleMakeBill">生成结算单
						{{selection.length>0 ?`(已选中${selection.length})`: '' }}</el-button>
					<el-date-picker v-model="search.dateRange" type="daterange" align="right" unlink-panels
						range-separator="至" start-placeholder="核销时间起" end-placeholder="核销时间止" value-format="YYYY-MM-DD">
					</el-date-picker>
					<div class="right-panel-search">
						<el-input style="width: 220px;" v-model="search.keyword" placeholder="单据编号/姓名/电话/活动名称"
							clearable></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch">查询</el-button>
					</div>
				</div>
				<div class="right-panel">
					<div class="right-panel-search">

					</div>
				</div>
			</el-header>
			<div>
				<el-card shadow="never">
					<ta-select-filter :data="selectFilter" :selected-values="selectedValues" :label-width="90"
						@on-change="stateChange"></ta-select-filter>
				</el-card>
			</div>
			<el-main class="nopadding">

				<el-container>
					<el-aside width="500px" v-show="type && type!=='待生成'">
						<scTable :pageSize="50" :params="{selectFilter:selectedValues}" ref="tableBill"
							:apiObj="apiCS.list" @selection-change="selectionBillChange" stripe remoteSort remoteFilter
							row-key="id" highlightCurrentRow @row-click="rowClick">
							<el-table-column type="selection" width="50"></el-table-column>
							<el-table-column label="批次号" width="165">
								<template #default="{row}">
									{{row.cs_batch_no}}
								</template>
							</el-table-column>
							<el-table-column label="总金额" width="80">
								<template #default="{row}">
									¥ {{row.cs_total_amount}}
								</template>
							</el-table-column>
							<el-table-column label="时间">
								<template #default="{row}">
									<p>生成: {{row.created_at}}</p>
									<p v-if="row.cs_settled_at">结算: {{row.cs_settled_at}}</p>
								</template>
							</el-table-column>
						</scTable>
					</el-aside>
					<el-main class="nopadding">

						<scTable :pageSize="100" :params="{selectFilter:selectedValues}" ref="table"
							:apiObj="apiObj.list" @selection-change="selectionChange" stripe remoteSort remoteFilter>
							<el-table-column type="selection" width="50"></el-table-column>
							<el-table-column label="序号" prop="id" width="50">
								<template #default="scope">
									<span @dblclick="table_edit(scope.row, scope.$index)"> {{scope.$index+1}}
									</span>
								</template>
							</el-table-column>
							<el-table-column label="订单详情" width="350">
								<template #default="{row}">
									<view>
										<view
											style="display: flex;flex-direction: row; gap: 15px; align-items: center;">

											<el-image style="max-width: 110px; max-height: 110px; border-radius: 5px;"
												preview-teleported hide-on-click-modal
												:src="row.order_item.product.product_image+'?x-oss-process=image/resize,w_110&t='"
												:preview-src-list="[row.order_item.product.product_image]" show-progress
												fit="contain" :z-index="19900501" />

											<view style="display: flex;flex-direction: column;">
												<view style="font-weight: 700;">
													<text>名称</text>
													<text style="color: #6666666;">
														&nbsp;{{row.order_item.product.product_title}}</text>
												</view>
												<view>
													<text style="font-weight: 700;">规格</text>
													<text
														style="color: #6666666;">&nbsp;{{row.order_item.product_sku.sku_title}}</text>
												</view>
												<view>
													<text style="font-weight: 700;">数量</text>
													<text
														style="color: #6666666;">&nbsp;x&nbsp;{{row.order_item.item_sku_quantity}}</text>
												</view>
												<view>
													<text style="font-weight: 700;">总额</text>
													<text
														style="color: #6666666;">&nbsp;{{row.order_item.item_sku_total_amount}}</text>
												</view>
												<view>
													<text style="font-weight: 700;">佣金</text>
													<text style="color: #6666666;">&nbsp;{{row.cr_amount}}</text>
												</view>
											</view>
										</view>
										<view>
											ID {{row.id}}
										</view>
									</view>
								</template>
							</el-table-column>
							<el-table-column label="归属详情" prop="user.account_phone" width="250">
								<template #default="{row}">
									<el-descriptions :column="1" border size="small" label-width="70">
										<el-descriptions-item label="佣金归属">
											<view
												style="display: flex; flex-direction: row; justify-content: space-between; align-items: center;">
												<text>{{row.user.account_phone}}</text>
												<el-popconfirm title="是否查询钉钉并绑定工号？"
													@confirm="searchAndBindEemployee(row.user.account_phone)">
													<template #reference>
														<el-icon style="cursor: pointer;">
															<Search />
														</el-icon>
													</template>
												</el-popconfirm>
											</view>
										</el-descriptions-item>
										<el-descriptions-item label="订单所属开发" v-if="false">

										</el-descriptions-item>
									</el-descriptions>
								</template>
							</el-table-column>
							<el-table-column label="订单核销时间" prop="cr_confirmed_at" width="150">
							</el-table-column>
							<el-table-column label="创建时间" prop="created_at">
							</el-table-column>
							<el-table-column label="操作" fixed="right" align="right" width="280" v-if="false">
								<template #default="scope">
									<template v-if="scope.row.wdrl_status=='approved'">
										<el-button-group>
											<el-popconfirm title="确定要发放吗？"
												@confirm="wxpayTransfer(scope.row, scope.$index)">
												<template #reference>
													<el-button text type="primary" size="small">微信发放</el-button>
												</template>
											</el-popconfirm>
											<el-button @click="wxpayQuery(scope.row, scope.$index)" text type="primary"
												size="small">查询付款单</el-button>
											<el-popconfirm title="取消付款单？"
												@confirm="wxpayCancel(scope.row, scope.$index)">
												<template #reference>
													<el-button text type="primary" size="small">取消付款单</el-button>
												</template>
											</el-popconfirm>
										</el-button-group>
									</template>
									<template v-if="scope.row.wdrl_status=='pending'">
										<el-button-group>
											<el-popconfirm title="通过审核？"
												@confirm="wdrlRecordApprove(scope.row, scope.$index)">
												<template #reference>
													<el-button text type="primary" size="small">通过</el-button>
												</template>
											</el-popconfirm>
											<el-button @click="showRejectDialog(scope.row, scope.$index)" text
												type="primary" size="small">拒绝</el-button>
										</el-button-group>
									</template>
									<template v-if="scope.row.wdrl_status=='rejected'">
										{{scope.row.wdrl_status_text}}
									</template>
								</template>
							</el-table-column>
						</scTable>


					</el-main>
				</el-container>


			</el-main>
		</el-container>
	</el-container>

	<!-- 拒绝理由弹窗 -->
	<el-dialog title="拒绝理由" v-model="rejectDialogVisible" width="500" v-if="false">
		<el-form :model="rejectForm" ref="rejectForm">
			<el-form-item label="拒绝理由" prop="reject_reason">
				<el-input type="textarea" :rows="4" v-model="rejectForm.reject_reason" placeholder="请输入拒绝理由"></el-input>
			</el-form-item>
		</el-form>

		<span slot="footer" class="dialog-footer">
			<el-button @click="rejectDialogVisible = false">取 消</el-button>
			<el-button type="primary" :loading="rejectLoading" @click="wdrlRecordReject">确 定</el-button>
		</span>
	</el-dialog>


	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
</template>

<script>
	import saveDialog from './save'

	export default {
		name: 'commission.bill',
		components: {
			saveDialog
		},
		data() {
			return {
				type: '待生成',
				settlement_id: null,
				rejectForm: {
					id: '',
					reject_reason: '',
				},
				rejectDialogVisible: false,
				rejectLoading: false,
				selectFilter: [{
					title: "状态",
					key: "type",
					options: [{
							label: "全部",
							value: null
						},
						{
							label: "待生成",
							value: "待生成",
							icon: "el-icon-bottom-left"
						},
						{
							label: "待结算",
							value: "待结算",
							icon: "el-icon-bottom-left"
						},
						{
							label: "已结算",
							value: "已结算",
							icon: "el-icon-flag"
						}
					]
				}, {
					title: "佣金为零?",
					key: "cr_amount",
					options: [{
						label: "全部",
						value: null
					}, {
						label: "为零",
						operator: '=',
						value: 0,
					}, {
						label: "不为零",
						operator: '!=',
						value: 0,
					}]
				}],
				dialog: {
					save: false
				},
				apiObj: this.$API.system.commissionRecord,
				apiCS: this.$API.system.commissionSettlement,
				selection: [],
				selectionBill: [],
				search: {
					keyword: null,
					dateRange: null
				}
			}
		},
		mounted() {

		},
		methods: {
			// 查询并绑定员工信息
			searchAndBindEemployee(phone) {
				this.$showLoading()
				this.$API.system.account.searchAndBindEemployee.post({
					phone: phone
				}).then(res => {
					this.$alert(res.msg, "提示", {
						type: res.code == 0 ? 'success' : 'error',
					})
				}).finally(() => {
					this.$hideLoading()
				})
			},
			// 点击结算单
			rowClick(e) {
				this.settlement_id = e.id;
				this.$refs.table.upData({
					settlement_id: e.id
				})
			},
			handleJieSuan() {
				let ids = this.selectionBill.map(item => item.id);
				console.error(ids)
			},
			handleMakeBill() {
				let ids = this.selection.map(item => item.id);
				this.$showLoading();
				this.$API.system.commissionSettlement.create.post({
					ids: ids
				}).then(res => {
					this.$alert(res.msg, "提示", {
						type: res.code == 0 ? 'success' : 'error',
					})
				}).finally(() => {
					this.$hideLoading();
				})
			},
			showRejectDialog(item) {
				this.rejectForm.id = item.id;
				this.rejectForm.reject_reason = '';
				this.rejectDialogVisible = true;
			},
			stateChange(e) {
				var params = {
					selectFilter: e
				};
				if (['已结算', '待结算'].includes(e.type[0].value)) {
					params.settlement_id = 'null';
				} else {
					params.settlement_id = null
				}
				this.type = e.type[0].value;
				this.$refs.table.upData(params)
				this.$refs.tableBill.upData(params)
			},
			//添加
			add() {
				this.dialog.save = true
				this.$nextTick(() => {
					const checkedNode = this.$refs.group.getCurrentNode();
					this.$refs.saveDialog.open().setData({
						category_id: checkedNode?.id || null
					});
				})
			},
			//编辑
			table_edit(row) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			//查看
			table_show(row) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('show').setData(row)
				})
			},
			//删除
			async table_del(row, index) {
				var reqData = {
					id: row.id
				}
				var res = await apiObj.remove.post(reqData);
				if (res.code == 0) {
					this.$refs.table.tableData.splice(index, 1);
					this.$message.success(res.msg)
				} else {
					this.$alert(res.message, "提示", {
						type: 'error'
					})
				}
			},
			//批量删除
			async batch_del() {
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning'
				}).then(() => {
					const loading = this.$loading();
					this.selection.forEach(item => {
						this.$refs.table.tableData.forEach((itemI, indexI) => {
							if (item.id === itemI.id) {
								this.$refs.table.tableData.splice(indexI, 1)
							}
						})
					})
					loading.close();
					this.$message.success("操作成功")
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection) {
				this.selection = selection;
			},
			selectionBillChange(selection) {
				this.selectionBill = selection;
			},
			//树点击事件
			groupClick(data) {
				this.$refs.table.upData({
					category_id: data.id
				})
			},
			//搜索
			upsearch() {
				this.$refs.table.upData(this.search)
			},
			//本地更新数据
			handleSuccess(data, mode) {
				if (mode == 'add') {
					this.$refs.table.refresh()
				} else if (mode == 'edit') {
					this.$refs.table.refresh()
				}
			}
		}
	}
</script>

<style>
</style>
